package com.compus_help.service.impl;

import com.compus_help.Mapper.OrderCategoryMapper;
import com.compus_help.common.PageResult;
import com.compus_help.entity.DTO.OrderCategoryDTO;
import com.compus_help.entity.DTO.OrderCategoryPageDTO;
import com.compus_help.entity.Pojo.OrderCategory;
import com.compus_help.service.adminOrderCategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class adminOrderCategoryServiceImpl implements adminOrderCategoryService {
    private static final Logger log = LoggerFactory.getLogger(adminOrderCategoryServiceImpl.class);

    @Autowired
    private OrderCategoryMapper OrderCategoryMapper;
    @Autowired
    private OrderCategoryMapper orderCategoryMapper;

    @Override
    public PageResult getOrderCategoryPage(OrderCategoryPageDTO orderCategoryPageDTO) {
        //设置分页
        PageHelper.startPage(orderCategoryPageDTO.getPage(),orderCategoryPageDTO.getPageSize());
        Page<OrderCategory> page = OrderCategoryMapper.getOrderCategoryPage(orderCategoryPageDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addCategory(OrderCategoryPageDTO orderCategoryPageDTO) {
        log.info("分类名称: {}", orderCategoryPageDTO.getCategoryName());
        //更新数据
        OrderCategory orderCategory = new OrderCategory();
        //拷贝数据到分类对象
        BeanUtils.copyProperties(orderCategoryPageDTO, orderCategory);
        log.info("添加分类: {}", orderCategory);
        log.info("分类名称: {}", orderCategory.getCategoryName());
        orderCategory.setCreateTime(LocalDateTime.now());
        orderCategory.setUpdateTime(LocalDateTime.now());
        //调用mapper接口插入数据
        OrderCategoryMapper.addCategory(orderCategory);
    }

    @Override
    public void changeStatus(String status, Integer categoryId) {
        log.info("分类状态变更为: {},{}",status,categoryId );
        OrderCategoryMapper.changeStatus(status,categoryId);
        log.info("状态变更成功");
    }

    @Override
    public void updateCategory(OrderCategoryDTO OrderCategoryDTO) {
        log.info("更新分类: {}", OrderCategoryDTO);
        OrderCategory orderCategory = new OrderCategory();
        //拷贝数据到分类对象
        BeanUtils.copyProperties(OrderCategoryDTO, orderCategory);
        orderCategory.setCategoryId(OrderCategoryDTO.getCategoryId());
        log.info("分类id: {}" , orderCategory.getCategoryId());
        log.info("快递名称: {}", orderCategory.getCategoryName());
        //更新数据
        orderCategory.setUpdateTime(LocalDateTime.now());
        OrderCategoryMapper.updateCategory(orderCategory);
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        orderCategoryMapper.deleteCategory(categoryId);
    }
}
